package manager.dao.proxy;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import cn.tool.Md5;
import manager.dao.impl.UserDAOImpl;
import manager.database.DatabaseSqlSession;
import manager.vo.User;

public class  UserDAOProxy{
	private UserDAOImpl dao = null;
	private UserDAOProxy() throws Exception{
		this.dao = new UserDAOImpl(DatabaseSqlSession.getSqlSession());
	}
	public static UserDAOProxy getInstance() throws Exception{
		return new UserDAOProxy();
	}
	//查询(id,userid,name,identity,start,end)
    public List<User> selectUser(Map<String,Object>map){
    	List<User> list = null;
    	try {
			list = this.dao.selectUser(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	finally{
    		DatabaseSqlSession.closeSqlSession();
    	}
    	return list;
    }
    //注册
    public boolean addUser(User user){
    	boolean flag = false;
    	Map<String,Object> map = new HashMap<String,Object>();
    	map.put("userid", user.getUserid());
    	try {
			if(this.dao.selectUser(map).size()==0){//返回的List集合不为空，只是里面没有对象
				if(this.dao.addUser(user)>0){
					flag = true;
					DatabaseSqlSession.commit();	
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    	finally{
    		DatabaseSqlSession.closeSqlSession();
    	}
    	return flag;
    }
    //登录,根据昵称或者userid
    public List<User> checkLogin(User user){
    	List<User> list = null;
    	try {
			list = this.dao.checkLogin(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	finally{
    		DatabaseSqlSession.closeSqlSession();
    	}
    	return list;
    }
    public static void main(String[]args){
    	Map<String,Object> map =new HashMap<String,Object>();
    	try {
			List<User> list = UserDAOProxy.getInstance().selectUser(map);
			for (User user : list) {
				System.out.println(user.toString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
    }
}
